<?php
include_once("functions/db_manipulate.php");

if (!init()) {
    redirect("?action=login");
    exit();
}

?>
<meta charset="UTF-8">
<?php


if ($_GET['page'] == '') {
    $start = 1;
    $page = 1;
} else {
    $start = ($_GET['page'] - 1) * 20 + 1;
    $page = $_GET['page'];
}

$newsResultSet = getNews($start, $start + 29);

$news = countNews();
$pages = pagesFromNews($news);

$page = $pages;

if ($pages <= 5) {
    $pagesStart = 1;
    $pagesEnd = $pages;
} else if ($pages >= 5 && $page + 2 <= $pages) {
    $pagesStart = $page - 2;
    $pagesEnd = $page + 2;
} else if ($pages >= 5 && $page + 2 > $pages) {
    $pagesStart = $pages - 4;
}

?>


<div class="userscontent">
    <div class="blockheader">
        Все новости:
    </div>

    <div id="pages">
        <?php
        if ($pages == 0 or $pages == 1) {

        } else {
            if ($page > 1) {
                ?>
                <div class="page"
                     onclick="location.href='?action=new&block=del&page=1'">
                    <<
                </div>
            <?php
            }

            for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
                ?>
                <div class="page"
                     onclick="location.href='?action=new&block=del&page=<?= $i ?>'">
                    <?= $i ?>
                </div>
            <?php
            }
            if ($page != $pages) {
                ?>
                <div class="page"
                     onclick="location.href='?action=new&block=del&page=<?= $pages ?>'">
                    >>
                </div>
            <?php
            }
        }
        ?>
    </div>

    <?php
    while ($new = mysql_fetch_assoc($newsResultSet)) {
        ?>
        <div class="user" onclick="location.href='show_new_news_messages.php?nid=<?= $new['id'] ?>'">
            <div class="usertd">
                <?= $new['header'] ?>
            </div>
            <div class="usertd">
                <?= $new['date'] ?>
            </div>
            <div class="usertd">
                <?= countNewsResponses($new['id']) ?>
            </div>

        </div>
        <div class="blockuserbutton" onclick="$('#askuserdelete<?= $new['id'] ?>').slideDown()">
            Удалить
        </div>

        <div id="askuserdelete<?= $new['id'] ?>" class="askuserdelete">
            Вы уверены, что хотите удалить данную новость ?<br>

            <div class="askuserdeletebutton" style="margin-left: 100px;"
                 onclick="location.href='?action=delnewhan&nid=<?= $new['id'] ?>'">
                Удалить
            </div>
            <div class="askuserdeletebutton" onclick="$('#askuserdelete<?= $new['id'] ?>').slideUp()">
                Отмена
            </div>

        </div>
    <?
    }
    ?>

</div>

